//
//  ServiceViewController.swift
//  AESOLAR_IOS
//
//  Created by Mr.Wang on 2019/10/16.
//  Copyright © 2019 Mr.Wang. All rights reserved.
//

import UIKit
import RxSwift
import RxDataSources
import RxCocoa

class ServiceViewController: BaseUIViewController {
    
    let tableView = UITableView.init(frame: CGRect.zero, style: .grouped)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "江苏新源".localized
        self.view.backgroundColor = #colorLiteral(red: 0.9724436402, green: 0.972609818, blue: 0.9724331498, alpha: 1)
        
        let imageView = UIImageView()
        imageView.clipsToBounds = true
        imageView.contentMode = .scaleAspectFill
        imageView.image = UIImage.init(named: "service_background")
        self.view.addSubview(imageView)
        imageView.snp.makeConstraints { (make) in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(SCREEN_WIDTH * 0.53)
        }
        
        let headerView = UIView.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_WIDTH * 0.25))
        let label = UILabel()
        label.textColor = .black
        label.text = "客服服务：".localized
        label.font = UIFont.systemFont(ofSize: 14, weight: .bold)
        headerView.addSubview(label)
        label.snp.makeConstraints { (make) in
            make.left.equalTo(13)
            make.bottom.equalTo(-15)
        }
        
        tableView.tableHeaderView = headerView
        tableView.tableFooterView = UIView.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 0.1))
        tableView.contentInset = .zero
        tableView.bounces = false
        tableView.separatorStyle = .none
        tableView.backgroundColor = UIColor.clear
        tableView.estimatedRowHeight = 0
        tableView.rowHeight = 155
        tableView.register(ServiceCell.self)
        self.view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        APPMessage.shared.unreadMessageCount.asObservable()
            .distinctUntilChanged()
            .map{ message -> [ServiceModel] in
                return [
                    ServiceModel(imageName: "service_1",
                                 name: "售后服务".localized,
                                 detail: "在线售后服务， 核实属实提供完整的解决方案！".localized,
                                 message: 0,
                                 vcType: ConnactViewController.self),
                    ServiceModel(imageName: "service_2",
                                 name: "在线留言".localized,
                                 detail: "联系我们，专业的客服人员及时为您提供解答！".localized,
                                 message: message,
                                 vcType: ChatViewController.self),
                    ServiceModel(imageName: "service_3",
                                 name: "报假信息".localized,
                                 detail: "无法查验组件信息，组件信息有误时，马上报假！".localized,
                                 message: 0,
                                 vcType: FakelistViewController.self)]
        }.bind(to: tableView.rx.items(cellType: ServiceCell.self)) { (index, model, cell) in
            cell.iconImageView.image = UIImage.init(named: model.imageName)
            cell.detailNameLabel.text = model.detail
            cell.nameLabel.text = model.name
            cell.countLabel.isHidden = model.message == 0
            cell.countLabel.text = "\(model.message)"
        }.disposed(by: rx.disposeBag)
        
        tableView.rx.modelSelected(ServiceModel.self)
            .subscribe(onNext: { [weak self] (model) in
                self?.navigationController?.pushViewController(model.vcType.init(), animated: true)
            }).disposed(by: rx.disposeBag)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.barStyle = .default
        self.navigationController?.navigationBar.tintColor = .black
        self.navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.black]
    }
    
    override func navigationBarStyle() -> NavigationBarStyle {
        return .clearColor
    }
    
}
